package com.tiankun.eurekaadmin.service.serviceImpl;


import com.tiankun.eurekaadmin.exception.CustomerException;
import com.tiankun.eurekaadmin.mapper.AdminMapper;
import com.tiankun.eurekaadmin.pojo.Admin;
import com.tiankun.eurekaadmin.service.AdminService;
import com.tiankun.eurekaadmin.util.Md5Utils;
import org.apache.catalina.Manager;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;

/**
 * @author 伍炳清
 * @date 2020-10-16 14:50
 */
@Service("adminService")
public class AdminServiceImpl implements AdminService {

    @Resource
    private AdminMapper adminMapper;


    @Override
    public void addAdmin(String phone, String password, String name) throws CustomerException {
        //判断是否传入管理员对象电话是否为空
        if (phone == null || phone.equals("")) {
            throw new CustomerException("手机号不能为空");
        }
        //判断是否传入管理员对象密码是否为空
        if (password == null || password.equals("")) {
            throw new CustomerException("密码不能为空");
        }
        if (name == null || name.equals("")) {
            throw new CustomerException("姓名不能为空");
        }

        Admin admin = new Admin();
        admin.setPhone(phone);
        //判断是否已注册
        Example example = new Example(Admin.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone", admin.getPhone());

        Admin adminByPhone = adminMapper.selectOneByExample(example);

        //已注册提示前往登录
        if (adminByPhone != null) {
            throw new CustomerException("手机号已注册");
        }

        //未注册过继续注册
        //生成盐
        String salt = Md5Utils.createSalt();

        //加密密码
        String md5Password = Md5Utils.md5Password(password, salt);
        admin.setPassword(md5Password);
        admin.setSalt(salt);
        admin.setName(name);
        adminMapper.insertSelective(admin);
    }

    @Override
    public Admin login(String phone, String password) throws CustomerException {

        //判断是否传入管理员对象电话是否为空
        if (phone == null || phone.equals("")) {
            throw new CustomerException("手机号不能为空");
        }
        //判断是否传入管理员对象密码是否为空
        if (password == null || password.equals("")) {
            throw new CustomerException("密码不能为空");
        }

        Admin admin = new Admin();
        admin.setPhone(phone);

        //判断是否已注册
        Example example = new Example(Admin.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone", admin.getPhone());
        Admin adminByPhone = adminMapper.selectOneByExample(example);
        //未注册
        if (adminByPhone == null) {
            throw new CustomerException("用户名或密码错误");
        }
        String md5Password = Md5Utils.md5Password(password, adminByPhone.getSalt());
        //已注册验证密码
        if (!adminByPhone.getPassword().equals(md5Password)) {

            throw new CustomerException("用户名或密码错误");

        }
        return adminByPhone;


    }

    @Override
    public void updatePassword(String phone, String newPassword) throws CustomerException {
        //判断是否传入管理员对象电话是否为空
        if (phone == null || phone.equals("")) {
            throw new CustomerException("手机号不能为空");
        }
        //判断是否传入管理员对象密码是否为空
        if (newPassword == null || newPassword.equals("")) {
            throw new CustomerException("新密码不能为空");
        }

        Admin admin = new Admin();
        admin.setPhone(phone);
        //查询id
        Example example = new Example(Admin.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone", admin.getPhone());
        Admin adminByPhone = adminMapper.selectOneByExample(example);

        admin.setId(adminByPhone.getId());
        //生成新盐
        String salt = Md5Utils.createSalt();
        //加密新密码
        String md5Password = Md5Utils.md5Password(newPassword, salt);

        admin.setPassword(md5Password);
        admin.setSalt(salt);
        adminMapper.updateByPrimaryKeySelective(admin);


    }
}
